package cn.cy.structure.composite.general;

/**
 * @author yangxq
 * @desc
 * @date 2025-05-20
 */
public class Folder {

    private String name;
    private File[] files;          // 仅能存储文件（无法包含子文件夹）
    private Folder[] subFolders;  // 仅能存储子文件夹（类型硬编码）

    public Folder(String name) {
        this.name = name;
        this.files = new File[0];
        this.subFolders = new Folder[0];
    }

    // 添加文件（仅支持文件）
    public void addFile(File file) {
        File[] newFiles = new File[files.length + 1];
        System.arraycopy(files, 0, newFiles, 0, files.length);
        newFiles[files.length] = file;
        this.files = newFiles;
    }

    // 添加子文件夹（仅支持文件夹）
    public void addFolder(Folder folder) {
        Folder[] newFolders = new Folder[subFolders.length + 1];
        System.arraycopy(subFolders, 0, newFolders, 0, subFolders.length);
        newFolders[subFolders.length] = folder;
        this.subFolders = newFolders;
    }

    // 打印目录结构（需分别处理文件和文件夹）
    public void printStructure(int depth) {
        System.out.println(" ".repeat(depth) + "+ " + name + " (Folder)");
        depth++;
        // 遍历文件
        for (File file : files) {
            file.printStructure(depth);
        }
        // 遍历子文件夹
        for (Folder folder : subFolders) {
            folder.printStructure(depth);
        }
    }

}
